﻿using System;
using System.Collections.Generic;
using System.IO;

namespace ParseControls
{
    public class ParseConfigPackage
    {
        // Fields 
        private static string _path = string.Empty;
        private static string _file = "parse-config-package-2015.xml";

        // Properties 
        private List<ParseApp> _Apps = new List<ParseApp>();
        public List<ParseApp> Apps
        {
            get { return _Apps; }
            set { _Apps = value; }
        }

        private List<ParseClass> _Classes = new List<ParseClass>();
        public List<ParseClass> Classes
        {
            get { return _Classes; }
            set { _Classes = value; }
        }

        private List<ParseProperty> _Properties = new List<ParseProperty>();
        public List<ParseProperty> Properties
        {
            get { return _Properties; }
            set { _Properties = value; }
        }

        // User 
        private string _UserName = "Account";
        public string UserName
        {
            get { return _UserName; }
            set { _UserName = value; }
        }

        private string _EMail = string.Empty;
        public string EMail
        {
            get { return _EMail; }
            set { _EMail = value; }
        }

        private string _ImageUrl = string.Empty;
        public string ImageUrl
        {
            get { return _ImageUrl; }
            set { _ImageUrl = value; }
        }

        private string _BackgroundImageUrl = string.Empty;
        public string BackgroundImageUrl
        {
            get { return _BackgroundImageUrl; }
            set { _BackgroundImageUrl = value; }
        }

        // ctr 
        public ParseConfigPackage()
        {

        }
        static ParseConfigPackage()
        {

        }

        // Methods 
        #region Methods
        public static ParseConfigPackage Load()
        {
            _path = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) + "\\";

            ParseConfigPackage pack = new ParseConfigPackage();
            try
            {
                if (File.Exists(_path + _file))
                {
                    string package = File.ReadAllText(_path + _file);

                    pack = ParseHelper.DeserializeFromString<ParseConfigPackage>(package);
                }

            }
            catch (Exception ex)
            {

            }

            return pack;
        }
        public void Save()
        {
            _path = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) + "\\";

            try
            {
                ParseConfigPackage pack = this as ParseConfigPackage;
                string package = ParseHelper.SerializeObject<ParseConfigPackage>(pack);

                if (File.Exists(_path + _file))
                {
                    FileInfo fi = new FileInfo(_path + _file);
                    File.Delete(_path + _file);
                }
                if (!File.Exists(_path + _file))
                {
                    File.WriteAllText(_path + _file, package);

                    // MessageBox.Show("Package successfully saved.");
                }
            }
            catch (Exception ex)
            {

            }
        }
        private bool Delete()
        {
            _path = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) + "\\";

            try
            {
                if (File.Exists(_path + _file))
                {
                    File.Delete(_path + _file);
                    return true;
                }
            }
            catch (Exception ex)
            {

            }

            return false;
        }
        internal bool Clear()
        {
            _path = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) + "\\";

            ParseConfigPackage pack = new ParseConfigPackage();
            try
            {
                if (File.Exists(_path + _file))
                {
                    string package = File.ReadAllText(_path + _file);
                    pack = ParseHelper.DeserializeFromString<ParseConfigPackage>(package);


                    pack.Save();

                    return true;
                }
            }
            catch (Exception ex)
            {

            }

            return false;
        }
        #endregion
    }

    public sealed class ParseOne
    {
        // Fields 
        private static volatile ParseOne instance;
        private static object syncRoot = new Object();

        // ctr 
        private ParseOne() { }
        public static ParseOne Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new ParseOne();
                    }
                }

                return instance;
            }
        }

        // Package 
        private static ParseConfigPackage _Package = null;
        public static ParseConfigPackage Package
        {
            get
            {
                if (ParseOne._Package == null)
                {
                    ParseOne.Package = ParseConfigPackage.Load();
                }

                return ParseOne._Package;
            }
            set
            {
                ParseOne._Package = value;
            }
        }
        public static void ReloadPackage()
        {
            ParseOne.Package = ParseConfigPackage.Load();
        }

        // Handler 
        public event ParseCustomInstanceEventHandler Change;
        public event ParseCustomInstanceEventHandler Changed;
        public event ParseCustomEnterClickEventHandler EnterClick;
        public event ParseCustomButtonClickEventHandler ButtonClick;
        public event ParseCustomInformationEventHandler InformationSend;

        // Events 
        private static void OnChange(ParseCustomEventArgs e)
        {
            if (ParseOne.Instance.Change != null)
                ParseOne.Instance.Change(ParseOne.Instance, e);
        }
        private static void OnChanged(ParseCustomEventArgs e)
        {
            if (ParseOne.Instance.Changed != null)
                ParseOne.Instance.Changed(ParseOne.Instance, e);
        }

        public static void EnterClicking(ParseCustomEventArgs args)
        {
            OnEnterClick(args);
        }
        private static void OnEnterClick(ParseCustomEventArgs e)
        {
            if (ParseOne.Instance.EnterClick != null)
                ParseOne.Instance.EnterClick(ParseOne.Instance, e);
        }

        public static void ButtonClicking(ParseCustomEventArgs args)
        {
            OnButtonClick(args);
        }
        private static void OnButtonClick(ParseCustomEventArgs e)
        {
            if (ParseOne.Instance.ButtonClick != null)
                ParseOne.Instance.ButtonClick(ParseOne.Instance, e);
        }

        public static void InformationSending(ParseCustomEventArgs args)
        {
            OnInformationSend(args);
        }
        private static void OnInformationSend(ParseCustomEventArgs e)
        {
            if (ParseOne.Instance.InformationSend != null)
                ParseOne.Instance.InformationSend(ParseOne.Instance, e);
        }
    }

    public class ParseCustomEventArgs : EventArgs
    {
        // Properties 
        private bool _IsHandled = false;
        public bool IsHandled
        {
            get { return _IsHandled; }
            set { _IsHandled = value; }
        }

        private string _Property = string.Empty;
        public string Property
        {
            get { return _Property; }
            set { _Property = value; }
        }

        private string _Title = string.Empty;
        public string Title
        {
            get { return _Title; }
            set { _Title = value; }
        }

        private string _Text = string.Empty;
        public string Text
        {
            get { return _Text; }
            set { _Text = value; }
        }

        private string _Command = string.Empty;
        public string Command
        {
            get { return _Command; }
            set { _Command = value; }
        }

        private int _Seconds = 0;
        public int Seconds
        {
            get { return _Seconds; }
            set { _Seconds = value; }
        }

        public string[] Parameters = null;

        private string _Parameter = string.Empty;
        public string Parameter
        {
            get { return _Parameter; }
            set { _Parameter = value; }
        }

        private object _Object = null;
        public object Object
        {
            get { return _Object; }
            set { _Object = value; }
        }

        private object _sender = null;
        public object Sender
        {
            get { return _sender; }
            set { _sender = value; }
        }

        private Guid parentGUID = Guid.Empty;
        public Guid ParentGUID
        {
            get { return parentGUID; }
            set { parentGUID = value; }
        }

        private Guid childGUID = Guid.Empty;
        public Guid ChildGUID
        {
            get { return childGUID; }
            set { childGUID = value; }
        }

        //private FwContent _ParentItem = null;
        //public FwContent ParentItem
        //{
        //    get { return _ParentItem; }
        //    set { _ParentItem = value; }
        //}

        private string _ReturnCommand = string.Empty;
        public string ReturnCommand
        {
            get { return _ReturnCommand; }
            set { _ReturnCommand = value; }
        }

        // ctr 
        public ParseCustomEventArgs()
        {
            this.IsHandled = false;
        }
        public ParseCustomEventArgs(string command = "")
        {
            IsHandled = false;
            Command = command;
        }
        public ParseCustomEventArgs(string command = "", string prop = "", string text = "", bool isHandled = false)
        {
            IsHandled = isHandled;
            Command = command;
            Property = prop;
            Text = text;
        }
        public ParseCustomEventArgs(string command = "", string parameter = "", bool isHandled = false)
        {
            IsHandled = isHandled;
            Command = command;
            Parameter = parameter;
        }
    }

    public delegate void ParseCustomEnterClickEventHandler(object sender, ParseCustomEventArgs e);
    public delegate void ParseCustomButtonClickEventHandler(object sender, ParseCustomEventArgs e);
    public delegate void ParseCustomInstanceEventHandler(object sender, ParseCustomEventArgs e);
    public delegate void ParseCustomInformationEventHandler(object sender, ParseCustomEventArgs e);

    // obsolete (evtl.) 
    public delegate void FwFileSelectedEventHandler(object sender, ParseCustomEventArgs e);
    public delegate void FwFileOpenedEventHandler(object sender, ParseCustomEventArgs e);
    public delegate void FwExpanderEventHandler(object sender, ParseCustomEventArgs e);
}
